package com.hksj.acm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.acm.dto.DeviceCoreParamConfigSaveOrUpdateDTO;
import com.hksj.acm.entity.DeviceCoreParamConfig;
import com.hksj.acm.mapper.DeviceCoreParamConfigMapper;
import com.hksj.acm.vo.DeviceCoreParamConfigVO;
import com.hksj.cbm.service.DeviceInstanceService;
import com.hksj.cbm.vo.DeriveMetadataVO;
import com.hksj.common.core.common.Constants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author fjt
 * @data 2024/7/11 11:49
 * @describe todo
 */
@Service
public class DeviceCoreParamConfigService extends ServiceImpl<DeviceCoreParamConfigMapper, DeviceCoreParamConfig> {

    @Autowired
    private DeviceInstanceService deviceInstanceService;


    /**
     * 查询指定设备的核心参数配置信息
     * @param deviceId
     * @return
     */
    public List<DeviceCoreParamConfigVO> queryDeviceCoreParamConfigByDeviceId(String deviceId){
        LambdaQueryWrapper<DeviceCoreParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceCoreParamConfig::getDeviceId, deviceId);
        queryWrapper.eq(DeviceCoreParamConfig::getDelFlag, Constants.ZERO);
        queryWrapper.orderByAsc(DeviceCoreParamConfig::getSort);
        List<DeviceCoreParamConfig> list = baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(list)){
            // 设备监测指标key : 指标相关信息
            Map<String, DeriveMetadataVO> attributeMap = deviceInstanceService.getDeviceAttribute(deviceId);
            List<DeviceCoreParamConfigVO> voList = new ArrayList<>(list.size());
            list.forEach(deviceCoreParamConfig -> {
                DeviceCoreParamConfigVO vo = DeviceCoreParamConfigVO.builder().id(deviceCoreParamConfig.getId())
                        .deviceId(deviceCoreParamConfig.getDeviceId()).property(deviceCoreParamConfig.getProperty())
                        .sort(deviceCoreParamConfig.getSort()).defaultIcon(deviceCoreParamConfig.getDefaultIcon())
                        .runIcon(deviceCoreParamConfig.getRunIcon()).ceaseIcon(deviceCoreParamConfig.getCeaseIcon())
                        .build();

                DeriveMetadataVO metadataVO = attributeMap.get(deviceCoreParamConfig.getProperty());
                if (ObjectUtil.isNotNull(metadataVO)){
                    vo.setPropertyName(metadataVO.getKeyName());
                    vo.setDataType(metadataVO.getDataType());
                    vo.setReadWriteStatus(metadataVO.getReadWriteStatus());
                }
                voList.add(vo);
            });
            return voList;
        }
        return null;
    }


    /**
     * 添加设备的核心参数配置
     * @param dto
     * @return
     */
    public String saveDeviceCoreParamConfig(DeviceCoreParamConfigSaveOrUpdateDTO dto){
        LambdaQueryWrapper<DeviceCoreParamConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceCoreParamConfig::getDeviceId, dto.getDeviceId());
        queryWrapper.eq(DeviceCoreParamConfig::getDelFlag, Constants.ZERO);
        int count = baseMapper.selectCount(queryWrapper);
        if (count >= Constants.SIX){
            // 一个设备最多只能加6个有效的核心参数的配置
            return "添加失败，当前设备的核心参数配置已达到6个";
        }

        queryWrapper.eq(DeviceCoreParamConfig::getProperty, dto.getProperty());
        int num = baseMapper.selectCount(queryWrapper);
        if (num >= Constants.ONE){
            return "添加失败，当前设备中已存在此核心参数的配置，不能重复添加";
        }

        DeviceCoreParamConfig config = new DeviceCoreParamConfig();
        BeanUtils.copyProperties(dto, config);
        return baseMapper.insert(config) > 0 ? null : "新增核心参数配置信息失败";
    }


    /**
     * 查询指定设备已经配置的物模型参数
     * @param deviceId
     * @return
     */
    public List<DeriveMetadataVO> queryProperties(String deviceId){
        Map<String, DeriveMetadataVO> map = deviceInstanceService.getDeviceAttribute(deviceId);
        if (CollUtil.isEmpty(map)){
            return null;
        }

        return new ArrayList<>(map.values());
    }


    /**
     * 更新设备的核心参数配置
     * @param dto
     * @return
     */
    public String updateDeviceCoreParamConfig(DeviceCoreParamConfigSaveOrUpdateDTO dto){
        Assert.notNull(dto.getId(), "配置id不能为空");
        DeviceCoreParamConfig config = baseMapper.selectById(dto.getId());
        if (Objects.isNull(config)){
            return "数据错误，系统中不存在此配置信息";
        }
        if (!config.getProperty().equals(dto.getProperty())){
            LambdaQueryWrapper<DeviceCoreParamConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DeviceCoreParamConfig::getDeviceId, config.getDeviceId());
            queryWrapper.eq(DeviceCoreParamConfig::getDelFlag, Constants.ZERO);
            queryWrapper.eq(DeviceCoreParamConfig::getProperty, dto.getProperty());
            int count = baseMapper.selectCount(queryWrapper);
            if (count > 0) {
                return "当前设备中已存在此核心参数配置";
            }
        }
        DeviceCoreParamConfig paramConfig = new DeviceCoreParamConfig();
        BeanUtils.copyProperties(dto, paramConfig);
        return baseMapper.updateById(paramConfig) > 0 ? null : "更新设备的核心参数配置信息失败";
    }









}
